package org.amphiprion.descentv2assistant.dao.db;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.descentv2assistant.entity.EncounterHero;
import org.amphiprion.descentv2assistant.entity.GameSession;

import android.content.Context;
import android.database.Cursor;

public class EncounterHeroDao extends AbstractDbDao {
	private static EncounterHeroDao instance;

	/**
	 * Hidden constructor.
	 * 
	 * @param context
	 *            the application context
	 */
	private EncounterHeroDao(Context context) {
		super(context);
	}

	/**
	 * Return the singleton.
	 * 
	 * @param context
	 *            the application context
	 * @return the singleton
	 */
	public static EncounterHeroDao getInstance(Context context) {
		if (instance == null) {
			instance = new EncounterHeroDao(context);
		}
		return instance;
	}

	public void persist(EncounterHero entity) {
		if (isInDb(entity.getId())) {
			update(entity);
		} else {
			create(entity);
		}
	}

	private void create(EncounterHero entity) {
		String sql = "insert into encounter_hero (" + "id, game_session_id, tag, hero_id, health, stamina, condition, xp, gold" + ") values (?,?,?,?,?,?,?,?,?)";

		Object[] params = new Object[9];
		params[0] = entity.getId();
		params[1] = entity.getGameId();
		params[2] = entity.getTag();
		params[3] = entity.getHero().getId();
		params[4] = entity.getHealth();
		params[5] = entity.getStamina();
		params[6] = entity.getConditionsForDB();
		params[7] = entity.getXp();
		params[8] = entity.getGold();

		execSQL(sql, params);

	}

	private void update(EncounterHero entity) {
		String sql = "update encounter_hero set game_session_id=?, tag=?, hero_id=?, health=?, stamina=?, condition=?, xp=?, gold=? where id=?";

		Object[] params = new Object[9];
		params[0] = entity.getGameId();
		params[1] = entity.getTag();
		params[2] = entity.getHero().getId();
		params[3] = entity.getHealth();
		params[4] = entity.getStamina();
		params[5] = entity.getConditionsForDB();
		params[6] = entity.getXp();
		params[7] = entity.getGold();
		params[8] = entity.getId();

		execSQL(sql, params);

	}

	public void writeNotes(EncounterHero entity, String notes) {
		String sql = "update encounter_hero set notes=? where id=?";

		Object[] params = new Object[2];
		params[0] = notes;
		params[1] = entity.getId();

		execSQL(sql, params);

	}

	public String readNotes(EncounterHero entity) {
		String sql = "select " + "notes from encounter_hero where id='" + entity.getId() + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		cursor.moveToFirst();
		String result = cursor.getString(0);
		cursor.close();
		return result;
	}

	public boolean isInDb(String id) {
		boolean result = false;
		String sql = "SELECT count(*) from encounter_hero where id='" + id + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		cursor.moveToFirst();
		result = cursor.getInt(0) != 0;
		cursor.close();
		return result;
	}

	public List<EncounterHero> getAll(GameSession gameSession) {
		String sql = "select " + "id, game_session_id, tag, hero_id, health, stamina, condition, xp, gold from encounter_hero where game_session_id='" + gameSession.getId() + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		ArrayList<EncounterHero> result = new ArrayList<EncounterHero>();

		if (cursor.moveToFirst()) {
			do {
				EncounterHero entity = new EncounterHero(cursor);
				result.add(entity);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return result;
	}

	public void delete(EncounterHero entity) {
		String sql = "delete from encounter_hero where id=?";

		Object[] params = new Object[1];
		params[0] = entity.getId();

		execSQL(sql, params);

	}

}
